import { createStore, combineReducers, applyMiddleware } from "redux";
import { carReducer } from "./car/carReducer";
import { userReducer } from "./user/userReducer";

import { persistStore, persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";

import logger from "redux-logger";
import thunk, { ThunkMiddleware } from 'redux-thunk'
const persistConfig = {
  key: "root",
  // 本地和存储
  storage,
  // 仅仅存储 car
  whitelist: ["car", "user"],
};

const reducer = combineReducers({
  car: carReducer,
  user: userReducer,
});

const persistedReducer = persistReducer(persistConfig, reducer);

const store = createStore(persistedReducer, applyMiddleware(logger, thunk as ThunkMiddleware<RootState>));

export const persistor = persistStore(store);

export type RootState = ReturnType<typeof reducer>;

export default store;
